#include <iostream>
#include <unistd.h>
#include <fstream>
#include <string>
#include "../logs/log.h"
#include <mysql/mysql.h>
#include <cstring>
#include <errno.h>

using namespace define_log;

int main()
{
    std::string methon = getenv("METHOD");
    std::string paramenter; //CGI 获取目标文章的主键，这里就只有目标文章的主键

    int post_length = 0;
    if (methon == "GET")
    {
        paramenter = getenv("PARAMENTER");
    }else if (methon == "POST")
    {
        //1.获取先出发来CGI的参数信息
        post_length = atoi(getenv("CONTENT-LENHTH"));
        while (post_length > 0)
        {
            char x = 'g';
            int bx =  read(0, &x, 1);
            if (bx == -1)
                FAIL("%s", strerror(errno));

            paramenter.push_back(x);
            post_length--;
        }
    }
 
    //2.解析正文，并给予运算。这里只用输出一个数来表示CGI结果
    //这里约定协议为：blog_id=1002
    std::string  key = paramenter.substr(paramenter.find('=') + 1);

    //登录连接数据库
    MYSQL *conn;
    MYSQL_RES *res_text;
    MYSQL_RES *res_title;
    MYSQL_ROW row_text;
    MYSQL_ROW row_title;

    const char *server = "139.9.104.185";
    const char *user = "afeng";
    const char *password = "hxf17377795861";
    const char *database = "blog_system";
    conn = mysql_init(NULL);

    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
        std::cerr << mysql_error(conn) << std::endl;
        mysql_close(conn);
        return 1;
    }
    mysql_set_character_set(conn, "utf8");
    // 指令
    std::string str = "select title from blog where blogId=";
    str += key;
    if (mysql_query(conn, str.c_str())) {
        std::cerr << mysql_error(conn) << std::endl;
        mysql_close(conn);
        return 1;
    }
    //文章题目
    res_title = mysql_store_result(conn);
    if (res_title == NULL) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        mysql_close(conn);
        return 1;
    }
    //获取文章题目
    std::string title;
    while ((row_text = mysql_fetch_row(res_title)) != NULL) {
        title += row_text[0];
    }

    //获取文章
    std::string str_text = "select content from blog where blogId=";
    str_text += key;
    if (mysql_query(conn, str_text.c_str())) {
        std::cerr << mysql_error(conn) << std::endl;
        mysql_close(conn);
        return 1;
    }
    res_text = mysql_store_result(conn);
    if (res_text == NULL) {
        fprintf(stderr, "%s", mysql_error(conn));
        mysql_close(conn);
        return 1;
    }
    std::string text;
    while ((row_text = mysql_fetch_row(res_text)) != NULL) {
        text += row_text[0];
    }
    //文章正文拼接
    std::string body;
    //拼接头
    std::ifstream infile("CGI/blog_response_01.html");
    if (!infile.is_open()) {
        std::cerr << "Failed to open file" << std::endl;
        return 1;
    }

    std::string line;
    while (std::getline(infile, line)) body += line;
    infile.close();
    //组接身体
    body += "<div class=\"article-title\">";
    body += title;
    body += "</div>";

    body += "<p>";
    body += text;
    body += "</p>";

    //拼接尾
    std::ifstream infile2("CGI/blog_response_02.html");
    if (!infile2.is_open()) {
        std::cerr << "Failed to open file" << std::endl;
        return 1;
    }
    std::string line2;
    while (std::getline(infile2, line2))body += line2;
    infile2.close();

    std::cerr << "向父进程发送正文" << std::endl;
    // 向父进程发送正文
    std::cout << body << std::endl;//设置终止符

    mysql_free_result(res_text);
    mysql_free_result(res_title);
    mysql_close(conn);


    //马上退出，有没有都差不多
    if (methon == "POST")
    {
        close(1);
        close(0);
    }
    return 0;
}